package d0318;

import java.util.concurrent.TimeUnit;

public class Resource {
    public synchronized void f(){//synchronized有了按顺序，没有争先恐后
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName()+"f()is running");//获得当前线程名
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void g(){
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName()+"g()is running");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void h(){//synchronized有了按顺序，没有争先恐后
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName()+"h()is running");//获得当前线程名
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Resource resource=new Resource();
        new Thread(()->resource.f()).start();
        new Thread(()->resource.g()).start();
        resource.h();

    }
}

/*
class MyThread extends Thread{
    Resource resource=new Resource();
    @Override
    public void run() {
        resource.f();
    }
}*/